package com.zhonghan.common.wechat.api;


import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.zhonghan.common.utils.HttpClientUtil;
import com.zhonghan.common.wechat.bean.menu.Menu;
import com.zhonghan.common.wechat.bean.menu.MenuButtons;
import com.zhonghan.common.wechat.bean.result.BaseResult;

/**
 * 自定义菜单API
 * @author zhangceven
 *
 */
@SuppressWarnings("unused")
public class MenuApi {

	private static Logger logger = Logger.getLogger(MenuApi.class);
	
	/**
	 * 创建菜单链接
	 */
	private static String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	
	/**
	 * 获取菜单链接
	 */
	private static String GET_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=";
	
	/**
	 * 删除菜单链接
	 */
	private static String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
	

	/**
	 * 通过菜单json数据创建微信公众号菜单
	 * @param access_token
	 * @param menuJson
	 * @return
	 */
	public static BaseResult createMenu(String access_token, String menuJson){
		
		String result = HttpClientUtil.doPostJson(CREATE_MENU_URL + access_token , menuJson);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
	}
	
	/**
	 * 通过传入菜单对象创建微信公众号菜单
	 * @param access_token
	 * @param menu
	 * @return
	 */
	public static BaseResult createMenu(String access_token, MenuButtons menuButtons){
		
		String menuJson = JSON.toJSONString(menuButtons);
		
		BaseResult baseResult = createMenu(access_token, menuJson);
		
		return baseResult;
		
	}
	
	
	/**
	 * 获取菜单
	 * @param access_token
	 * @return
	 */
	public static Menu getMenu(String access_token){
		
		String result = HttpClientUtil.doGet(GET_MENU_URL + access_token);
		
		Menu menu = JSON.parseObject(result,Menu.class);
		
		return menu;
	}
	
	/**
	 * 删除菜单
	 * @param access_token
	 * @return
	 */
	public static BaseResult deleteMenu(String access_token){
		
		String result = HttpClientUtil.doGet(DELETE_MENU_URL + access_token);
		
		BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
		
		return baseResult;
	}
	
	
	
	
}
